﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;
using System.Globalization;

namespace Video_JS.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        string VideoHTML;
const string height_text = "height";
const string settings_text = "settings";
const string url_to_poster_text = "url_to_poster";
const string url_to_video_text = "url_to_video";
const string VideoType_text = "VideoType";
const string width_text = "width";

        protected override void CreateChildControls()
        {
            if (!String.IsNullOrEmpty(VideoURL)) {
                VideoHTML = "<link href=\"/_layouts/15/video-js/video-js.css\" rel=\"stylesheet\" type=\"text/css\">" +
                            "<script src=\"/_layouts/15/video-js/video.js\"></script>" +
                            "<script>" +
                            "videojs.options.flash.swf = \"/_layouts/15/video-js/video-js.swf\";" +
                            "</script>"+
                            "<video id=\""+VideoURL+"\" class=\"video-js vjs-default-skin\" controls preload=\"none\" width=\""+VideoWidth+"\" height=\""+VideoHeight+"\"" ;
                if (!String.IsNullOrEmpty(PosterURL)) {
                    VideoHTML += " poster=\""+PosterURL+"\"";
                }
                VideoHTML += " data-setup=\"{}\">";
                VideoHTML += "<source src=\"" + VideoURL + "\" type='video/"+VideoType+"' /></video>";                
                this.Controls.Add(new HtmlGenericControl("div") { InnerHtml = VideoHTML });
            }
        }

        static public string getLocalizedValue(string strInput)
        {
            // Function to retreive specified Language Variation Value
            string strLocalizedValue = "";

            strLocalizedValue = Microsoft.SharePoint.Utilities.SPUtility.GetLocalizedString("$Resources: " + strInput, "vsProject\\vsProject", (uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);

            return strLocalizedValue;
        }

        public static class Utility
        {

            // Wraps the SPUtility method of the same name.
            public static string GetLocalizedString(string resourceName, int LCID)
            {
                if (string.IsNullOrEmpty(resourceName))
                    return string.Empty;

                // SPUtility.GetLocalized string needs a resource expression as the first argument.
                string resourceExpression = string.Format("$Resources:{0}", resourceName);

                string resourceFile = "Video-JS";

                // Note: If the named resource does not have a value for the specified language, 
                // SPUtility.GetLocalizedString returns the value for the invariant language.
                // If the named resource does not exist, it returns the original expression.
                return SPUtility.GetLocalizedString(resourceExpression, resourceFile, (uint)LCID);
            }
        }
        
        public sealed class LocalizedCategoryAttribute : CategoryAttribute
        {
            public LocalizedCategoryAttribute(string category)
                : base(category)
            { }

            // Override this method to return values from the webpart's resource file.
            protected override string GetLocalizedString(string value)
            {
                return Utility.GetLocalizedString(value, CultureInfo.CurrentUICulture.LCID);
            }            
        }

        public sealed class LocalizedWebDisplayNameAttribute : WebDisplayNameAttribute
        {
            bool m_isLocalized;

            public LocalizedWebDisplayNameAttribute(string displayName)
                : base(displayName)
            { }

            // Override this property to return values from the webpart's resource file.
            public override string DisplayName
            {
                get
                {
                    if (!m_isLocalized)
                    {
                        this.DisplayNameValue = Utility.GetLocalizedString(base.DisplayName, CultureInfo.CurrentUICulture.LCID);
                        m_isLocalized = true;
                    }
                    return base.DisplayName;
                }
            }
        }

        [WebBrowsable(true),
        LocalizedWebDisplayName(url_to_video_text),
        Personalizable(PersonalizationScope.Shared),
        LocalizedCategory(settings_text)]
        public string VideoURL { get; set; }
    
        [WebBrowsable(true),
        LocalizedWebDisplayName(height_text),
        Personalizable(PersonalizationScope.Shared),
        LocalizedCategory(settings_text)]
        public string VideoHeight { get; set; }

        [WebBrowsable(true),
        LocalizedWebDisplayName(width_text),
        Personalizable(PersonalizationScope.Shared),
        LocalizedCategory(settings_text)]
        public string VideoWidth { get; set; }
        
        [WebBrowsable(true),        
        LocalizedWebDisplayName(url_to_poster_text),
        Personalizable(PersonalizationScope.Shared),
        LocalizedCategory(settings_text)]
        public string PosterURL { get; set; }

        public enum VideoTypeEnum
        {
            MP4,
            WebM,            
            OGG
        }

        [WebBrowsable(true),
        LocalizedWebDisplayName(VideoType_text),        
        Personalizable(PersonalizationScope.Shared),
        LocalizedCategory(settings_text)]
        public VideoTypeEnum VideoType { get; set; }

    }
}
